/**
 * Created By xiyuan
 * Author server@xiyuan.name (惜缘叛逆)
 * DateTime 2020/9/1 11:53 AM
 * Describe javascript功能描述
 * MIT License http://www.opensource.org/licenses/mit-license.php
 */

export function docxGen(outFile) {
	
	const officegen = require('officegen');
	const fs = require('fs');
	const docx = officegen('docx');
	
	// 生成docx文档成功的监听
	docx.on('finalize', function (written) {
		console.log('文档生成完毕')
	})
	
	// 文档生成错误的监听
	docx.on('error', function (err) {
		console.log(err)
	})
	
	// 创建header
	const header = docx.getHeader().createP({align: ('center')});
	
	header.addText('dominos代码采样', {font_size: 12, font_face: 'SimSun'});
	// 添加底线
	header.addHorizontalLine();
	
	return {
		add(text, option = {}) {
			let pObj = docx.createP();
			pObj.options.align = option.align || 'left';
			pObj.addText(text, option || {font_size: 12, font_face: 'KaiTi_GB2312'})
		},
		// 生成
		generate() {
			
			let out = fs.createWriteStream('example.docx' || outFile);
			
			out.on('error', function (err) {
				console.log(err)
			})
			// Async call to generate the output file:
			docx.generate(out)
		}
	}
}